package demo;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

public class Test2 {
    public static void main(String[] args) throws IOException {
        //点名器 70% 男生 随机100万次 查看结果是否 接近 7:3
        //随机点名
        //1.获取到数据集合
        BufferedReader br = new BufferedReader(new FileReader("IOTest\\names.txt"));
        //2.打乱集合
        ArrayList<String> list = new ArrayList<>();
        String len;
        while ((len = (br.readLine())) != null) {
            list.add(len);
        }
        br.close();
        //将男女分开
        //创建男女集合
        ArrayList<String> boyList = new ArrayList<>();
        ArrayList<String> girlList = new ArrayList<>();
        for (String s : list) {
            if("男".equals(s.split("-")[1])){
                boyList.add(s);
            } else {
                girlList.add(s);
            }
        }
        //概率抽取
        int[] arr = new int[100];
        for (int i = 0; i < 70; i++) {
            arr[i] = 1;
        }
        for (int i = 0; i < 30; i++) {
            arr[70 + i] = 0;
        }
        //将数组打乱顺序
        Random r = new Random();
        for (int i = 0; i < arr.length; i++) {
            int random = r.nextInt(100);
            int temp = arr[random];
            arr[i] = arr[random];
            arr[random] = temp;
        }
        //统计比例
        HashMap<String,Integer> hs = new HashMap<>();
        //循环
        for (int i = 0; i < 1000000; i++) {
            int index = r.nextInt(100);
            int n = arr[index];
            if(n == 1){
                Collections.shuffle(boyList);
                String s = boyList.get(0);
                if(hs.containsKey("男")){
                    int count = hs.get("男");
                    count++;
                    hs.put("男",count);
                } else {
                    hs.put("男",1);
                }
            } else {
                Collections.shuffle(girlList);
                String s = girlList.get(0);
                if(hs.containsKey("女")){
                    int count = hs.get("女");
                    count++;
                    hs.put("女",count);
                } else {
                    hs.put("女",1);
                }
            }
        }
        System.out.println(hs);

    }
}
